﻿using Blogger.UI.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace Blogger.UI.Controllers
{
    public class UploadController : UIController
    {
       
        public ActionResult AzureBlob()
        {
            return View();
        }
        public ActionResult BasicUpload()
        {
            return View();
        }
        public ActionResult AdvanceUpload()
        {
            return View();
        }
        [HttpPost]
        public ActionResult SaveFile(HttpPostedFileBase file)
        {
            //some file upload magic

            // return JSON
            return Json(new
            {
                name = "picture.jpg",
                type = "image/jpeg",
                size = 123456789
            });
        }
        private string StorageRoot
        {
            get { return Path.Combine(Server.MapPath("~/Files")); }
        }

        //DONT USE THIS IF YOU NEED TO ALLOW LARGE FILES UPLOADS
        [HttpGet]
        public void Delete(string id)
        {
            var filename = id;
            var filePath = Path.Combine(Server.MapPath("~/Files"), filename);

            if (System.IO.File.Exists(filePath))
            {
                System.IO.File.Delete(filePath);
            }
        }

        //DONT USE THIS IF YOU NEED TO ALLOW LARGE FILES UPLOADS
        [HttpGet]
        public void Download(string id)
        {
            var filename = id;
            var filePath = Path.Combine(Server.MapPath("~/Files"), filename);

            var context = HttpContext;

            if (System.IO.File.Exists(filePath))
            {
                context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
                context.Response.ContentType = "application/octet-stream";
                context.Response.ClearContent();
                context.Response.WriteFile(filePath);
            }
            else
                context.Response.StatusCode = 404;
        }

        //DONT USE THIS IF YOU NEED TO ALLOW LARGE FILES UPLOADS
        [HttpPost]
        public ActionResult UploadFiles()
        {
            var r = new List<ViewDataUploadFilesResult>();

            foreach (string file in Request.Files)
            {
                var statuses = new List<ViewDataUploadFilesResult>();
                var headers = Request.Headers;
                string clientFileName = headers["X-File-Name"];
                //if (string.IsNullOrEmpty(headers["X-File-Name"]))
                //{
                //    UploadWholeFile(Request, statuses);
                //}
                //else
                //{
                //    UploadPartialFile(headers["X-File-Name"], Request, statuses);
                //}
                if (string.IsNullOrEmpty(clientFileName))
                {
                    WriteBlob(Request, statuses);
                }
                else
                {
                    WritePartialBlob(clientFileName, Request, statuses);
                }
                JsonResult result = Json(statuses);
                result.ContentType = "text/plain";

                return result;
            }

            return Json(r);
        }

       
    }
}
